home *** CD-ROM | disk | FTP | other *** search
- /* CPROG10.CPP - Count the length of a string */
- #include <stdio.h>
-
- int stringlen(char *string) // Parameter is a pointer to the start of
- // the string whose length is to be counted.
- {
- char *t; // Temporary pointer
- t=string; // Point t at start of string.
- while( *t++ ) // While the thing pointed to by t is true (non-zero), increment t
- ; // Do nothing
- return (t-string-1); // t points to 1 past '\0', so subtract 1
- // from difference between address in t and
- // address of start of string
- }
-
- void main(void)
- (
- static char name[]="Count me!"; // Statics can be initialised during compilation
- printf("%d\n", stringlen(name) );
- )
-